from enum import Enum
from typing import Optional, List, Dict, Any, Union
from pydantic import BaseModel
from app.schemas.metrics_schema import Chart, BarChart, ScatterPlot, LineChart
from app.schemas.assets_schema import Text, Ontology, KnowledgeGraph, GraphPattern, GraphRule, MediaFile, TextRule


# 1. 任务状态
class TaskStatus(str, Enum):
    PENDING = "PENDING"
    RUNNING = "RUNNING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"


# 2. 产品类型
class ProductType(str, Enum):
    TEXT = "TEXT"
    FILE = "FILE"
    TEXT_RULE = "TEXT_RULE"
    GRAPH_RULE = "GRAPH_RULE"
    ONTOLOGY = "ONTOLOGY"
    KNOWLEDGE_GRAPH = "KNOWLEDGE_GRAPH"
    GRAPH_PATTERN = "GRAPH_PATTERN"


# HGPMS算法的输入参数
class HGPMSInputParams(BaseModel):
    graph_data: Dict[str, Any]  # 图数据（节点和边的信息）
    min_support: float = 0.05  # 最小支持度
    max_pattern_size: int = 4  # 最大模式大小
    node_types: Optional[List[str]] = None  # 节点类型列表
    edge_types: Optional[List[str]] = None  # 边类型列表


# 模式表示
class PatternRepresentation(BaseModel):
    node_types: Dict[str, int]  # 节点类型及数量
    edge_patterns: List[Dict[str, Any]]  # 边模式列表
    support: float  # 支持度
    instances_count: int  # 实例数量


# 模式实例
class PatternInstance(BaseModel):
    center_node: str  # 中心节点
    nodes: List[str]  # 实例中的节点
    edges: List[Dict[str, Any]]  # 实例中的边
    similarity: float  # 与模式的相似度


# HGPMS算法的输出参数
class HGPMSOutputParams(BaseModel):
    frequent_patterns: List[PatternRepresentation]  # 发现的频繁模式
    pattern_growth_history: List[int]  # 模式增长历史
    pattern_instances: Dict[str, List[PatternInstance]]  # 每个模式的实例
    algorithm: str = "HGPMS"  # 算法名称
    parameters: Dict[str, Any]  # 算法参数


# 3. 输入参数
class InputParams(BaseModel):
    hgpms_params: Optional[HGPMSInputParams] = None


# 4. 输出参数
class OutputParams(BaseModel):
    hgpms_results: Optional[HGPMSOutputParams] = None


# 5. 算法请求
class AlgorithmRequest(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    input_params: InputParams


# 6. 算法中间响应
class AlgorithmMiddleResponse(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    input_params: InputParams
    error_message: Optional[str] = None

    metrics: List[Chart] = []


# 7. 算法最终响应
class AlgorithmResponse(BaseModel):
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: int = 0
    task_logs: Optional[str] = None

    error_message: Optional[str] = None

    output_params: OutputParams

    metrics: List[Chart] = [] 